package com.zmj.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSON;
import com.zmj.anno.MyAjax;
import com.zmj.anno.RequestPath;
import com.zmj.dao.impl.AddressDaoImpl;
import com.zmj.entity.Address;
import com.zmj.entity.Region;
import com.zmj.service.IAddressService;
import com.zmj.service.impl.AddressServiceImpl;
import com.zmj.utils.Message;

public class AddressController {
	
	IAddressService addressService=new AddressServiceImpl();
	
	@RequestPath("/selAddress.shop")
	@MyAjax
	public String selAddress(HttpServletRequest request){
		String superCode=request.getParameter("superCode");
		List<Region> list=addressService.selRegion(superCode);
		
		return JSON.toJSONString(list);
	}
	@RequestPath("/addAddress.shop")
	@MyAjax
	public String addAddress(HttpServletRequest request){
	
		String aname=request.getParameter("aname");
		String province=request.getParameter("province");
		String city=request.getParameter("city");
		String district=request.getParameter("district");
		String amobile=request.getParameter("amobile");
		String aemail=request.getParameter("aemail");
		String aremark=request.getParameter("aremark");
		String string=request.getParameter("isDefault");
		int isDefault=Integer.parseInt(string);
		Address address=new Address(1, province, city, district, aname, aemail, amobile, aremark, isDefault);
		boolean flg=addressService.addAddress(address);
		Message m=new Message();
		if (flg) {
			m.setCode(1);
			m.setMes("地址添加成功");
			
		}else {
			m.setCode(0);
			m.setMes("地址添加失败");
		}
		return JSON.toJSONString(m);
	}
	
	@RequestPath("/topay.shop")
	public String topay(HttpServletRequest request) {
//		User user=(User) request.getSession().getAttribute("user");
//		if (null==user) {
//			return "login.jsp";
//		}else {
			List<Address> addressList=addressService.selAddressesByUserId(1);
			request.setAttribute("addressList",addressList);

			//获取复选框的值
			String[] gids = request.getParameterValues("gids");
			
			//将选中的商品id,放到session里面
			request.getSession().setAttribute("gids", gids);
			return "checkout.jsp";
//		}
	}
}
